package com.polaris.live.im.manager

import androidx.lifecycle.LifecycleOwner
import com.polaris.live.common.resp.UserAccountExtResp
import com.polaris.live.common.resp.UserGameAccountExtBean
import com.polaris.live.common.resp.UserLevelAccountResp
import com.polaris.live.im.sfs.core.SmartFoxManager
import com.polaris.live.resp.back_resp.PartyGameSettlementResp
import com.polaris.live.utils.LifecycleListenerDispatcher
import kotlinx.coroutines.withContext

/**
 * ImNoticeManager
 *
 * @author Created by lucas on 2023/11/1 09:52
 */
object ImNoticeManager {

    private val accountChangeDispatcher =
        LifecycleListenerDispatcher<OnAccountChangeListener>("accountChangeDispatcher")

    private val gameAccountChangeDispatcher =
        LifecycleListenerDispatcher<OnGameAccountChangeListener>("gameAccountChangeDispatcher")

    private val gameResultDispatcher =
        LifecycleListenerDispatcher<OnGameResultListener>("gameResultDispatcher")

    private val levelAccountChangeDispatcher =
        LifecycleListenerDispatcher<OnLevelResultListener>("levelAccountChangeDispatcher")

    /**
     * 添加用户余额变化监听
     *
     * 会自动取消
     */
    fun observeAccountChange(owner: LifecycleOwner, l: OnAccountChangeListener) {
        accountChangeDispatcher.observe(owner, l)
    }

    /**
     * 添加用户游戏余额变化监听
     */
    fun observeGameAccountChange(owner: LifecycleOwner, l: OnGameAccountChangeListener) {
        gameAccountChangeDispatcher.observe(owner, l)
    }

    /**
     * 添加用户豪魅值变化监听
     */
    fun observeLevelAccountChange(owner: LifecycleOwner, l: OnLevelResultListener) {
        levelAccountChangeDispatcher.observe(owner, l)
    }

    /**
     * 添加用户游戏结算监听  只有房间关闭后做结算通知
     */
    fun observeGameResult(owner: LifecycleOwner, l: OnGameResultListener) {
        gameResultDispatcher.observe(owner, l)
    }


    suspend fun onReceivGameResultMessage(data: List<PartyGameSettlementResp>) {
        withContext(SmartFoxManager.noticeDispatcher) {
            gameResultDispatcher.listeners.forEach { it.onGameResult(data) }
        }
    }

    internal suspend fun dispatchAccountChange(account: UserAccountExtResp) {
        withContext(SmartFoxManager.noticeDispatcher) {
            accountChangeDispatcher.listeners.forEach {
                it.onAccountChange(account)
            }
        }
    }

    internal suspend fun dispatchGameAccountChange(account: UserGameAccountExtBean) {
        withContext(SmartFoxManager.noticeDispatcher) {
            gameAccountChangeDispatcher.listeners.forEach {
                it.onAccountChange(account)
            }
        }
    }

    internal suspend fun dispatchLevelAccountChange(account: UserLevelAccountResp) {
        withContext(SmartFoxManager.noticeDispatcher) {
            levelAccountChangeDispatcher.listeners.forEach {
                it.onLevelResult(account)
            }
        }
    }
}

fun interface OnAccountChangeListener {
    fun onAccountChange(account: UserAccountExtResp)

}

fun interface OnGameAccountChangeListener {
    fun onAccountChange(account: UserGameAccountExtBean)
}

fun interface OnGameResultListener {
    fun onGameResult(data: List<PartyGameSettlementResp>)
}

fun interface OnLevelResultListener {
    fun onLevelResult(account: UserLevelAccountResp)
}